<?php
include_once 'admin_check.php'; 
$cates = getAll('article_cate','1');
$is_show = array('隐藏','显示');
//get请求 判断是什么页面
$eid = isset($_GET['eid']) && !empty($_GET['eid'])? $_GET['eid']: '';
if($eid){ //如果是编辑页面
  $art = getOne('article',"art_id = $eid");
}

//post请求
if($_POST){
  $eid = isset($_POST['id']) && !empty($_POST['id'])? $_POST['id']: '';

  //接收数据
  $data['art_title'] = trim($_POST['art_title']);
  $data['art_author'] = trim($_POST['art_author']);
  $data['art_cate_id'] = intval($_POST['art_cate_id']);
  $data['art_desc'] = trim($_POST['art_desc']);
  $data['art_content'] = htmlspecialchars(trim($_POST['art_content']));
  $data['create_time']= isset($_POST['create_time'])&& !empty($_POST['create_time'])?strtotime($_POST['create_time']):time();
  $data['update_time']= isset($_POST['update_time'])&& !empty($_POST['update_time'])?strtotime($_POST['update_time']):time();
  $data['is_show'] = intval($_POST['is_show']);
  
  //如果有文件传入
  if($_FILES['art_img']['name'] != ''){
    //设置存储目录
    $uploads = '../uploads/';
    //如果是编辑页面
    if($eid){
      // 先判断图片是否更新
      if($_FILES['art_img']['name'] != $_POST['old_img']){ //如果更新了
        // 则先删除原来的图片
        $old_img = $uploads . $_POST['old_img'];
        if(file_exists($old_img)){
          $r = unlink($old_img);
          if(!$r){
            show_msg('原图删除失败');
          }
        }
        //再进行插入
        // 调用helper中的方法，并获得返回的新名字
        $filename = upload_file('art_img', $uploads);
        // 判断是否有返回新名称
        if( $filename !== false) {
          $data['art_img'] = $filename;
        }else{
          show_msg('图片上传失败');
        }

      }else{ //如果没有更新,则什么都不做
        // $data['art_img'] = $_POST['art_img'];
      }

    }else{//如果是添加页面，则直接进行插入
      // 调用helper中的方法，并获得返回的新名字
      $filename = upload_file('art_img', $uploads);
      // 判断是否有返回新名称
      if( $filename !== false) {
        $data['art_img'] = $filename;
      }else{
        show_msg('图片上传失败');
      }
    }
    
  }

  //将数据插入数据库
  //先判断是编辑还是添加
  if($eid){//如果是编辑
    $r = update('article',$data,"art_id = {$eid}");
    if($r){
      show_msg('更新成功');
    }else{
      show_msg('更新失败');
    }

  }else{ //如果是添加
    
    $r = insert('article',$data);

    if($r){
      show_msg('插入成功','article_list.php');
    }else{
      show_msg('插入失败');
    }

  }
}
?>
<?php include_once"header.php" ?>
<link rel="stylesheet" type="text/css" href="css/bootstrap-datetimepicker.css">
    <div id="topbar" class="affix">
      <ol class="breadcrumb">
        <li><a href="index.php"><span class="glyphicon glyphicon-home"></span></a></li>
        <li class="active">添加文章分类</li>
      </ol>
    </div>
    <div class="container">

	 <div class="row">
        <div class="col-md-10 col-lg-11 center-column">
        <form action="" method="post" class="cmxform" enctype="multipart/form-data">
        	<div class="panel">
            <div class="panel-heading">
              <div class="panel-title">添加文章</div>
              <div class="panel-btns pull-right margin-left">
              <a href="article_list.php" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicon glyphicon-chevron-left"></span></a>
            	  </div>
            </div>
            <div class="panel-body">
            	  <div class="col-md-7">
                  <div class="form-group">
                    <div class="input-group"> <span class="input-group-addon">文章标题</span>
                      <input type="text" name="art_title" value="<?php echo $art['art_title']?>" class="form-control">
                    </div>
                  </div>
                  <div class="form-group">
                    <div class="input-group"> <span class="input-group-addon">文章作者</span>
                      <input type="text" name="art_author" value="<?php echo $art['art_author']?>" class="form-control">
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="input-group"> <span class="input-group-addon">文章分类</span>
                      <select class="form-control" name='art_cate_id'>
                        <?php foreach ($cates as $key => $value){ ?>
                          <option value='<?php echo $value['acate_id']?>' <?php echo $art['art_cate_id'] == $value['acate_id']?'selected':'';?>><?php echo $value['acate_name']?></option>
                        <?php } ?>
                      </select>
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="input-group"> <span class="input-group-addon">是否显示</span>
                      <select class="form-control" name='art_cate_id'>
                        <?php foreach ($is_show as $key => $value){ ?>
                         <option value="<?php echo $key ?>" <?php echo $key == 1 ? 'selected' : '' ?>><?php echo $value ?></option>
                        <?php } ?>
                      </select>
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="input-group"> <span class="input-group-addon">文章图片</span>
                     <input type="file" name="art_img" value="<?php echo $art['art_img']?>" class="form-control" id ='upload'>
                     <!-- 隐藏的值，存储原来的图片 -->
                     <input type="hidden" name="old_img" value="<?php echo $art['art_img']?>">
                    </div>
                    <div id="preview">
                        <?php echo isset($art['art_img']) && !empty($art['art_img']) ? "<img src='../uploads/{$art['art_img']}' style='width:300px;margin-top:10px;' />":'';?>
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="input-group"> <span class="input-group-addon">发布时间</span>
                    <div class="input-group date create_time col-md-12"  data-date-format="yyyy-mm-dd hh:ii:ss" data-link-field="create_time">
                      <input class="form-control" type="text" value="<?php echo isset($art['create_time']) ? date('Y-m-d H:i:s',$art['create_time']) : date('Y-m-d H:i:s', time()); ?>" readonly>
                      <span class="input-group-addon">
                        <span class="glyphicon glyphicon-remove"></span>
                      </span>
                      <span class="input-group-addon">
                        <span class="glyphicon glyphicon-th"></span>
                      </span>
                    </div>
                    <input type="hidden" name="create_time" value="<?php echo $art['create_time']?>" class="form-control">
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="input-group"> <span class="input-group-addon">更新时间</span>
                     <div class="input-group date update_time col-md-12"  data-date-format="yyyy-mm-dd hh:ii:ss" data-link-field="update_time">
                      <input class="form-control" type="text" value="<?php echo isset($art['update_time']) ? date('Y-m-d H:i:s',$art['update_time']) : date('Y-m-d H:i:s', time()); ?>" readonly>
                      <span class="input-group-addon">
                        <span class="glyphicon glyphicon-remove"></span>
                      </span>
                      <span class="input-group-addon">
                        <span class="glyphicon glyphicon-th"></span>
                      </span>
                    </div>
                    <input type="hidden" name="update_time" value="<?php echo $art['update_time']?>" class="form-control">
                    </div>
                  </div>

                </div>

                <div class="form-group col-md-12">
                  <p class="text-primary">文章简介：</p>
                  <textarea name='art_desc' style="width:100%;height:150px;"><?php echo $art['art_desc']?></textarea>
                </div>

                <div class="form-group col-md-12">
                  <p class="text-primary">文章内容：</p>
                  <script style="width:800px;height:200px;" id="art_content" name="art_content" type="text/plain"></script>
                </div>

                <div class="col-md-7">
	                <div class="form-group">
                    <input type="hidden" name="id" value="<?php echo $art['art_id']?>">
	                  <input type="submit" value="提交" class="submit btn btn-blue">
	                </div>
                </div>
            </div>
          </div>
          </form>
        </div>
    </div>
  </section>
  <!-- End: Content --> 
</div>

</body>

</html>

<!-- 时间插件 -->
<script type="text/javascript" src="js/bootstrap-datetimepicker.js"></script>
<script type="text/javascript" src="js/bootstrap-datetimepicker.zh-CN.js"></script>
<script type="text/javascript">
  $('.create_time').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1
    });
  $('.update_time').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1
    });
</script>

<!-- 编辑器插件 -->
<script type="text/javascript" src="js/ueditor/ueditor.config.js" ></script>
<script type="text/javascript" src="js/ueditor/ueditor.all.min.js" ></script>
<script type="text/javascript">
  var ue = UE.getEditor('art_content');
  ue.ready(function(){
    var content = '<?php echo isset($art['art_content']) ? htmlspecialchars_decode($art['art_content']):''; ?>';
    ue.setContent(content);

  })
</script>

<!-- 预览图 -->
<script type="text/javascript">
  //获取按钮
  var fbtn = document.querySelector('#upload');

  //当按钮内容改变时触发事件
  fbtn.onchange = function() {
    console.log(this.files && this.files[0]);
    if(this.files && this.files[0]) {
      var file = this.files[0];
      // 生成文件的url
      var url = createObjectURL(file);
      console.log(url);
      //获得显示区域
      var preview = document.querySelector('#preview');
      //在显示区域显示图片
      preview.innerHTML = '<img style="width:300px;;margin-top:10px;" src="'+url+'"/>';

      resolveObjectURL(file);
    }
  }

  var createObjectURL = function(blob){
    return window[window.webkitURL ? 'webkitURL' : 'URL']['createObjectURL'](blob);
  };
  var resolveObjectURL = function(blob){
    window[window.webkitURL ? 'webkitURL' : 'URL']['revokeObjectURL'](blob);
  };
</script>

